package com.example.extendanimationdemo;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.LinearLayout.LayoutParams;

public class LoginExpandAnimation extends Animation {

	private View mAnimationView = null;
	private LayoutParams mViewLayoutParams = null;
	private int mStart = 0;
	private int mEnd = 0;
	private boolean isShown = false;

	public LoginExpandAnimation(View view, boolean Shown) {
		isShown = Shown;
		animationSettings(view, 400);
	}

	public LoginExpandAnimation(View view, int duration, boolean Shown) {
		isShown = Shown;
		animationSettings(view, duration);
	}

	private void animationSettings(View view, int duration) {
		setDuration(duration);
		mAnimationView = view;
		mViewLayoutParams = (LayoutParams) view.getLayoutParams();
		mStart = mViewLayoutParams.bottomMargin;
		mEnd = isShown ? 0 : (0 - view.getHeight());
		// mEnd = (mStart == 0 ? (0 - view.getHeight()) : 0);
		view.setVisibility(View.VISIBLE);
	}

	@Override
	protected void applyTransformation(float interpolatedTime, Transformation t) {
		super.applyTransformation(interpolatedTime, t);
		if (interpolatedTime < 1.0f) {
			mViewLayoutParams.bottomMargin = mStart + (int) ((mEnd - mStart) * interpolatedTime);
			mAnimationView.requestLayout();
		} else {
			mViewLayoutParams.bottomMargin = mEnd;
			mAnimationView.requestLayout();
			if (mEnd != 0) {
				mAnimationView.setVisibility(View.GONE);
			}
		}
	}

}
